package com.qn.wechat.controller;

import com.jeesuite.common.json.JsonUtils;
import com.jeesuite.mybatis.plugin.pagination.Page;
import com.jeesuite.springweb.model.WrapperResponse;
import com.oneplatform.base.exception.AssertUtil;
import com.oneplatform.base.exception.ExceptionCode;
import com.oneplatform.base.model.PageResult;
import com.qn.wechat.ifservice.dto.QRChannelDto;
import com.qn.wechat.ifservice.dto.SBPromoteDto;
import com.qn.wechat.ifservice.dto.para.QRChannelQueryPara;
import com.qn.wechat.ifservice.dto.para.SBPromoteQeuryPara;
import com.qn.wechat.service.QRChannelService;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.ws.ResponseWrapper;

@Controller
@RequestMapping(value = "/qrchannel")
public class QRChannelController {

    @Resource
    QRChannelService qrChannelService;

    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    @ApiOperation(value = "获取渠道列表")
    public @ResponseBody
    PageResult<QRChannelDto> getList(@ModelAttribute QRChannelQueryPara para) {

        Page<QRChannelDto> page = qrChannelService.getList(para);
        return new PageResult<QRChannelDto>(page.getPageNo(), page.getPageSize(), page.getTotal(), page.getData());

    }

    @ApiOperation(value = "获取渠道")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public @ResponseBody
    WrapperResponse<QRChannelDto> get(@ModelAttribute QRChannelQueryPara para) {

        WrapperResponse<QRChannelDto> response = new WrapperResponse<>();
        para.setPageNo(1);
        para.setPageSize(1);
        AssertUtil.isTrue(para.getId()!=0 , ExceptionCode.REQUEST_PARAM_ERROR.code, "id不能为空");

        Page<QRChannelDto> page = qrChannelService.getList(para);
        if (page != null && page.getData() != null && page.getData().size() > 0) {
            response.setData(page.getData().get(0));
        }

        return response;

    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存渠道。id=0的时候代表新增")
    public @ResponseBody
    WrapperResponse<String> save(@RequestBody QRChannelDto dto) {
        System.out.print(JsonUtils.toJson(dto));
        return qrChannelService.save(dto);
    }

}
